package priv.mill.demo;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSConsumer {

	private static final String brokerURL = "tcp://mill.one:61616";//ActiveMQConnection.DEFAULT_BROKER_URL;
	private static final String password = ActiveMQConnection.DEFAULT_PASSWORD;
	private static final String userName = ActiveMQConnection.DEFAULT_USER;

	public static void main(String[] args) {
		/**
		 * 实例化连接工厂
		 */
		Connection createConnection = null;
		try {
			ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(userName, password, brokerURL);
			createConnection = connectionFactory.createConnection();//创建连接
			createConnection.start();//开启连接
			Session session = createConnection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//消费不需要事务
			Topic queue = session.createTopic("FirstTopic");//队列名和生产者一致
			MessageConsumer messageConsumer = session.createConsumer(queue);//创建消息消费者
			messageConsumer.setMessageListener(new MyListener());
			Thread.sleep(Integer.MAX_VALUE);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(createConnection!=null)
			try {
				createConnection.close();
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
		
	}

}
